﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Gnosis.Alexandria.Data;

namespace Gnosis.Alexandria.Views
{
    public partial class SearchView : NavigateView
    {
        public SearchView()
        {
            InitializeComponent();
            listView.ContextMenuStrip = WorkContentMenu;
        }

        private static string GetYouTubeSearchUrl(string search)
        {
            const string youTubeFormat = "http://www.youtube.com/results?search_category=10&search_query={0}&search_duration=short&search_type=videos&suggested_categories=10,24&uni=3";

            var normalized = search ?? string.Empty;
            normalized = normalized.Trim();
            normalized = normalized.Replace(' ', '+');
            normalized = normalized.Replace("&", "%26");

            return string.Format(youTubeFormat, normalized);
        }

        private static string GetWikipediaSearchUrl(string search)
        {
            const string wikipediaFormat = "http://en.wikipedia.org/wiki/{0}";

            var normalized = search ?? string.Empty;
            normalized = normalized.Trim();
            normalized = normalized.Replace(' ', '_');
            normalized = normalized.Replace("&", "%26");

            return string.Format(wikipediaFormat, normalized);
        }

        private static string GetAllMusicDotComUrl(string search)
        {
            const string allMusicDotComFormat = "http://allmusic.com/search/artist/{0}";

            var normalized = search ?? string.Empty;
            normalized = normalized.Trim();
            normalized = normalized.Replace(' ', '+');
            normalized = normalized.Replace("&", "%26");

            return string.Format(allMusicDotComFormat, normalized);
        }

        private static string GetLyricWikiUrl(string search)
        {
            const string lyricWikiFormat = "http://lyrics.wikia.com/{0}";

            var normalized = search ?? string.Empty;
            normalized = normalized.Trim();
            normalized = normalized.Replace(' ', '_');
            normalized = normalized.Replace("&", "%26");

            return string.Format(lyricWikiFormat, normalized);
        }

        private static string GetGrooveSharkUrl(string search)
        {
            const string grooveSharkFormat = "http://listen.grooveshark.com/#/search/songs/?query={0}";

            var normalized = search ?? string.Empty;
            normalized = normalized.Trim();
            normalized = normalized.Replace(" ", "%20");
            normalized = normalized.Replace("&", "%2526");

            return string.Format(grooveSharkFormat, normalized);
        }

        protected override void HandleMessage(IMessageOld message)
        {
            switch (message.Type)
            {
                case MessageTypes.ResponseNavigateViewSearch:
                {
                    listView.Items.Clear();
                    var resultData = message.Body as SearchResultData;
                    var details = resultData.Result as IList<SearchDetailData>;
                    foreach (var detail in details)
                    {
                        var item = new ListViewItem();
                        item.Tag = detail.Id;
                        item.Text = "Artist"; //detail.Type;
                        item.SubItems.Add(detail.Name);
                        item.SubItems.Add(detail.From);
                        var date = detail.Date != DateTime.MinValue && detail.Date != DateTime.MaxValue ? detail.Date.ToString("yyyy") : "N/A";
                        item.SubItems.Add(date);
                        item.SubItems.Add(detail.Note);
                        listView.Items.Add(item);
                    }
                    break;
                }
                case MessageTypes.RequestSearchViewEdit:
                {
                    if (listView.SelectedItems != null && listView.SelectedItems.Count > 0)
                    {
                        var type = listView.SelectedItems[0].Text;
                        var id = (long)listView.SelectedItems[0].Tag;

                        //TODO: Move this check somewhere else
                        if (type == "Artist")
                            Controller.Send(this, MessageTypes.RequestArtistEditLoad, new EditRequestData { Id = id, Source = this.Name });
                    }
                    break;
                }
                case MessageTypes.RequestSearchYouTube:
                {
                    var url = GetYouTubeSearchUrl(listView.SelectedItems[0].SubItems[1].Text);
                    Controller.Send(this, MessageTypes.RequestBrowseViewLoad, new NavigateToUrlData { Url = url, Site = "YouTube"});
                    break;
                }
                case MessageTypes.RequestSearchWikipedia:
                {
                    var url = GetWikipediaSearchUrl(listView.SelectedItems[0].SubItems[1].Text);
                    Controller.Send(this, MessageTypes.RequestBrowseViewLoad, new NavigateToUrlData { Url = url, Site = "Wikipedia" });
                    break;
                }
                case MessageTypes.RequestSearchAllMusicDotCom:
                {
                    var url = GetAllMusicDotComUrl(listView.SelectedItems[0].SubItems[1].Text);
                    Controller.Send(this, MessageTypes.RequestBrowseViewLoad, new NavigateToUrlData { Url = url, Site = "AllMusic.com"});
                    break;
                }
                case MessageTypes.RequestSearchLyricWiki:
                {
                    var url = GetLyricWikiUrl(listView.SelectedItems[0].SubItems[1].Text);
                    Controller.Send(this, MessageTypes.RequestBrowseViewLoad, new NavigateToUrlData { Url = url, Site = "LyricWiki" });
                    break;
                }
                case MessageTypes.RequestSearchGrooveshark:
                {
                    var url = GetGrooveSharkUrl(listView.SelectedItems[0].SubItems[1].Text);
                    Controller.Send(this, MessageTypes.RequestBrowseViewLoad, new NavigateToUrlData { Url = url, Site = "Grooveshark" });
                    break;
                }
                default:
                    base.HandleMessage(message);
                    break;
            }
        }
    }
}
